package net.xumf.web.controller.member.user;


import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import net.xumf.domain.master.SystemUser;
import net.xumf.service.master.SystemUserService;
import net.xumf.support.auth.CurrentUser;
import net.xumf.support.enums.CodeEnum;
import net.xumf.support.exception.SystemException;
import net.xumf.support.model.ResponseResult;
import net.xumf.support.model.dto.PageDTO;
import net.xumf.support.model.dto.PasswordChangeDTO;
import net.xumf.support.model.dto.PasswordChangeMineDTO;
import net.xumf.support.model.dto.UserPageDTO;
import net.xumf.support.model.vo.PageVO;
import net.xumf.web.controller.common.BaseControllerImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@Slf4j
@RestController
@Api(value = "用户管理", description = "用户管理")
@RequestMapping("member/users")
public class SystemUserController extends BaseControllerImpl<UserPageDTO, SystemUserService, SystemUser, Long> {

    @ApiOperation(value = "修改用户密码", notes = "修改用户密码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataTypeClass = Long.class, paramType = "param"),
            @ApiImplicitParam(name = "password", value = "新密码", required = true, dataTypeClass = String.class, paramType = "param")
    })
    @PutMapping("/{id}/password")
    public ResponseResult passwordChange(@Valid @RequestBody PasswordChangeDTO passwordChangeDTO, BindingResult result) {
        if (result.hasErrors()) {
            FieldError fieldError = result.getFieldError();
            return ResponseResult.failure(fieldError.getDefaultMessage());
        }
        service.passwordChange(passwordChangeDTO);
        return ResponseResult.success();
    }

    @ApiOperation(value = "修改用户邮箱", notes = "修改用户邮箱")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataTypeClass = Long.class, paramType = "param"),
            @ApiImplicitParam(name = "password", value = "新密码", required = true, dataTypeClass = String.class, paramType = "param")
    })
    @PostMapping("/mail/change")
    public ResponseResult mailChange(String email) {
        if (StringUtils.isBlank(email)) {
            throw new SystemException(CodeEnum.REQUEST_PARAM_ERROR);
        }
        SystemUser user = CurrentUser.instance().get();
        user.setEmail(email);
        service.dynamicSave(user);
        return ResponseResult.success();
    }

    @ApiOperation(value = "修改用户密码", notes = "修改用户密码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataTypeClass = Long.class, paramType = "param"),
            @ApiImplicitParam(name = "password", value = "新密码", required = true, dataTypeClass = String.class, paramType = "param")
    })
    @PostMapping("/password/change/mine")
    public ResponseResult passwordChangeMine(@Valid PasswordChangeMineDTO passwordChangeDTO, BindingResult result) throws Exception {
        if (result.hasErrors()) {
            FieldError fieldError = result.getFieldError();
            return ResponseResult.failure(fieldError.getDefaultMessage());
        }
        service.passwordChangeMine(passwordChangeDTO);
        return ResponseResult.success();
    }

    @ApiOperation(value = "根据用户名获取用户信息列表")
    @GetMapping("/get/all/username")
    public PageVO getByModuleIdAndPage(@ApiParam PageDTO pageDTO) {
        return service.getByUsername(pageDTO);
    }

    @Override
    public PageVO getByPage(UserPageDTO pageDTO) {
        return service.getByStatusAndSearchText(pageDTO);
    }
}